package com.yanqu.road.server.logic.bag;

import com.yanqu.road.entity.bag.UseGoodsResult;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eMonopolyMarketResType;
import com.yanqu.road.entity.log.newmonopolymarket.NewMonopolyMarketUserPhysicalChangeLogData;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.newmonopolymkt.NewMonopolyMarketModule;
import com.yanqu.road.server.manger.NewMonopolyMarketCommonMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.systemgroup.newmonopolymarket.NewMonopolyMarketMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

/**
 * 增加新榷场关引
 */
public class UseRecoverNewMonopolyMarketPhysical extends BaseUseGoods {

    public UseRecoverNewMonopolyMarketPhysical(GamePlayer player, GoodsInfo goodsInfo, long param) {
        super(player, goodsInfo, param);
    }
    @Override
    int checkUse(long count) {
        if(!NewMonopolyMarketMgr.canEnterSystem(player)){
            return GameErrorCode.E_SYSTEM_NO_OPEN;
        }
        if(!NewMonopolyMarketCommonMgr.isSystemOpenTime(eMonopolyMarketResType.RES_KING_NEW.getValue())
                && !NewMonopolyMarketCommonMgr.isSystemOpenTime(eMonopolyMarketResType.RES_QUEEN_NEW.getValue())){
            return GameErrorCode.E_NEW_MONOPOLY_MARKET_RES_GRAB_SYSTEM_NO_OPEN_NO_USE_PROP;
        }
        NewMonopolyMarketModule marketModule = player.getModule(NewMonopolyMarketModule.class);
        if(marketModule.userData == null){
            return GameErrorCode.E_SYSTEM_NO_OPEN;
        }
        return 0;
    }

    @Override
    UseGoodsResult executeUse(long count) {
        NewMonopolyMarketModule marketModule = player.getModule(NewMonopolyMarketModule.class);
        UseGoodsResult result = new UseGoodsResult();
        BigInteger min = goodsInfo.getParamList().get(0);
        int addValue = (int)(min.intValue() * count);
        //不刷其他, 保险起见，只刷此刻的体力恢复
        //NewMonopolyMarketMgr.refreshUser(player,true,false);

        int oldValue;
        synchronized (marketModule) {
            NewMonopolyMarketMgr.refreshGuanYin(marketModule.userData, DateHelper.getCurrentSecond(), true);
            oldValue = marketModule.userData.getGuanYin();
            marketModule.userData.setGuanYin(marketModule.userData.getGuanYin() + addValue);
            NewMonopolyMarketMgr.refreshGuanYin(marketModule.userData, DateHelper.getCurrentSecond(), true);
        }

        player.getModule(NewMonopolyMarketModule.class).synRoleData();

        result.setReward(PropertyHelper.parseGoodsToString(GameConfig.NEWMONOPOLYMARKET_GUANYIN_ID, BigInteger.valueOf(addValue)));
        AutoLogMgr.add(new NewMonopolyMarketUserPhysicalChangeLogData(player.getUserId(), marketModule.userData.getWeekId(),
                marketModule.userData.getOfficeCurPos(),
                marketModule.userData.getOfficeLevel(),
                marketModule.userData.getOfficeNo(),
                3,
                oldValue,
                marketModule.userData.getGuanYin()));

        return result;
    }
}
